﻿''============================================================================
''OMANE - Paramétrage de la segmentation retenue pour l'étude du taux garanti
''Copyright © 2010 PIERRE TISSOT CONSEIL / NATIXIS ASSURANCES
''=============================================================================

Imports PTIC_GestionBDD
Imports System.Drawing.Drawing2D
Imports PTIC_Util

''' <summary>
''' Fenêtre permettant de paramétrer le degré de finesse souhaité pour l'étude du taux garanti
''' </summary>
''' <remarks></remarks>
Public Class frmDonneesSegmentation
    Implements PTIC_Util.IObserver

#Region "Variables de classe"

    'Objets métiers récupérés depuis l'ossature
    Protected _universBDD As UniversOMANE

    'Variables utilisées pour l'affichage
    Protected Const WM_SCROLL As Integer = &H115S

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
       (ByVal hwnd As Integer, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As Object) As Integer

    Protected _ltvSource As ListView
    Protected _ltvDest As ListView
    Protected mintScrollDirection As Integer

#End Region

#Region "Constructeur"

    Public Sub New(ByVal universBDD As UniversOMANE)

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        'Initialisation des composants métier
        Me._universBDD = universBDD

        'Initialisation des variables globales liées aux fonctions d'affichage
        Me._ltvSource = Nothing

    End Sub

#End Region

#Region "Gestion des listView permettant de sélectionner les segments"

    Private Sub ltv_varSeg_ItemDrag(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.ItemDragEventArgs) _
            Handles ltv_VarSegPoss.ItemDrag, ltv_varSegRetenues.ItemDrag

        _ltvSource = CType(sender, ListView)
        Try
            If Not _ltvSource.SelectedItems Is Nothing Then
                _ltvSource.DoDragDrop(_ltvSource.SelectedItems, DragDropEffects.Move)
            End If
        Catch ex As Exception
            MsgBox("Erreur méthode itemDrag " & ex.Message)
        End Try

    End Sub

    Private Sub ltv_VarSeg_DragEnter(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.DragEventArgs) _
            Handles ltv_VarSegPoss.DragEnter, ltv_varSegRetenues.DragEnter
        Try
            For i As Integer = 0 To e.Data.GetFormats().Length - 1
                If e.Data.GetFormats()(i).Equals _
                    ("System.Windows.Forms.ListView+SelectedListViewItemCollection") Then
                    e.Effect = e.AllowedEffect
                End If
            Next
        Catch ex As Exception
            MsgBox("Erreur méthode dragEnter " & ex.Message)
        End Try

    End Sub

    Private Sub ltv_VarSeg_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
            Handles ltv_VarSegPoss.DragDrop, ltv_varSegRetenues.DragDrop

        Try
            _ltvDest = CType(sender, ListView)

            'Récupération des items sélectionnés
            Dim draggedItems As ListView.SelectedListViewItemCollection = _ltvSource.SelectedItems

            'Création du dictionnaire de correspondances à transférer
            Dim lcorrespAajouter As New List(Of Correspondance)
            For Each draggedItem As ListViewItem In draggedItems
                lcorrespAajouter.Add(_universBDD.listeTraitements.getTraitTbleEncoursPrep1.ListeChampsSortie.Item(draggedItem.Text))
            Next

            'Mise à jour des segmentations possibles / retenus du mapping "TXGAR1"
            If Not (_ltvSource Is _ltvDest) And Not (lcorrespAajouter.Count = 0) Then
                If _ltvDest Is ltv_varSegRetenues Then
                    _universBDD.Segmentation.modifSegRetenue(lcorrespAajouter, operationCollection.ADD)
                End If
                If _ltvDest Is ltv_VarSegPoss Then
                    _universBDD.Segmentation.modifSegRetenue(lcorrespAajouter, operationCollection.REMOVE)
                End If
                _ltvDest.Select()
            End If

        Catch ex As Exception
            MsgBox("Erreur méthode dragDrop " & ex.Message)
        End Try

    End Sub

#End Region

#Region "MAJ de la dgv présentant l'extraction des segments à l'entrée de l'onglet"

    'MAJ de l'extraction à l'entrée de l'onglet si une correspondance a été modifiée
    Public Sub tbp_Segmentation_Enter()
        If Not _universBDD.getTraitement("traitReqSegmentation") Is Nothing Then
            Dim traitReqSegmentation As RequeteBDD = CType(_universBDD.getTraitement("traitReqSegmentation"), RequeteBDD)
            If traitReqSegmentation.etatExecDataTable = valueEtatCalcul.A_MAJ Then
                traitReqSegmentation.execute_DataTableToDgv(True, dgv_visuSegments)
            End If
        End If
    End Sub

#End Region

#Region "Actualisations de l'écran liées à l'observation d'objets métiers"

    ''' <summary>
    ''' Redirection des actualisations demandées par les objets observés
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="notification"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function actualise(ByVal sender As Object, ByVal notification As Object) As PTIC_Util.resultatValidationContraintes Implements PTIC_Util.IObserver.actualise

        If TypeOf sender Is Segmentation Then
            actualiseFromSegmentation(notification)
        End If
        Return Nothing
    End Function

    ''' <summary>
    ''' Gestion des actualisations demandées par l'objet segmentation
    ''' </summary>
    ''' <param name="notification"></param>
    ''' <remarks></remarks>
    Private Sub actualiseFromSegmentation(ByVal notification As Object)

        Dim notifModifSegmentation As Segmentation.notification = notification.notification
        Dim lSegAmodifier As List(Of Correspondance) = notification.listeSegments

        Dim dicoListSegments As New Dictionary(Of String, Correspondance)
        For Each corresp As Correspondance In lSegAmodifier
            dicoListSegments.Add(corresp.description, corresp)
        Next

        If notifModifSegmentation = OMANE.Segmentation.notification.AjoutSegmentsPoss Then
            Dim operation As operationCollection = operationCollection.ADD
            ltvFunc_modifCollCorresp(ltv_VarSegPoss, dicoListSegments, operation)
        End If
        If notifModifSegmentation = OMANE.Segmentation.notification.SuppressionSegmentsPoss Then
            Dim operation As operationCollection = operationCollection.REMOVE
            ltvFunc_modifCollCorresp(ltv_VarSegPoss, dicoListSegments, operation)
        End If

        If notifModifSegmentation = Segmentation.notification.ModifSegmentsRetenus Then
            Dim operation As operationCollection = notification.operation
            If operation = operationCollection.ADD Then
                ltvFunc_modifCollCorresp(ltv_varSegRetenues, dicoListSegments, operationCollection.ADD)
                ltvFunc_modifCollCorresp(ltv_VarSegPoss, dicoListSegments, operationCollection.REMOVE)
            End If
            If operation = operationCollection.REMOVE Then
                ltvFunc_modifCollCorresp(ltv_varSegRetenues, dicoListSegments, operationCollection.REMOVE)
                ltvFunc_modifCollCorresp(ltv_VarSegPoss, dicoListSegments, operationCollection.ADD)
            End If
            _universBDD.listeTraitements.getTraitReqSegmentation.execute_DataTableToDgv(True, dgv_visuSegments)
        End If

    End Sub

#End Region

End Class